package com.cxs.controller;

import com.cxs.entity.College;
import com.cxs.service.AdminService;
import com.cxs.service.ClassroomService;
import com.cxs.service.CollegeService;
import com.cxs.service.SeatOrderService;
import com.cxs.service.SeatService;
import com.cxs.entity.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

/*
 * @Project:seat-subscribe-sys
 * @Author:cxs
 * @Motto:放下杂念,只为迎接明天更好的自己
 * */
@Controller
@RequestMapping("/college")
public class CollegeController {

    @Autowired
    private AdminService adminService;

    @Autowired
    private ClassroomService classroomService;

    @Autowired
    private SeatService seatService;

    @Autowired
    private SeatOrderService seatOrderService;

    @Autowired
    private CollegeService collegeService;


    @RequestMapping("/toCollegeList")
    public String toCollegeList(HttpSession session, Model model) {
        List<College> list = collegeService.list(null);
        model.addAttribute("list", list);
        return "admin/collegeList";
    }

    @RequestMapping("/toAddCollege")
    public String toAddCollege(HttpSession session, Model model) {
        return "admin/addCollege";
    }

    @RequestMapping("/toEditCollege")
    public String toEditCollege(Integer id, HttpSession session, Model model) {
        College byId = collegeService.getById(id);
        model.addAttribute("data", byId);
        return "admin/editCollege";
    }

    @RequestMapping("/addCollege")
    @ResponseBody
    public Result addCollege(College college, HttpSession session, Model model) {
        Result result = new Result(200, "OK");
        boolean save = collegeService.save(college);
        if (!save) {
            result.setCode(-1);
            result.setMsg("添加失败");
        }
        return result;
    }

    @RequestMapping("/updateCollege")
    @ResponseBody
    public Result updateCollege(College college, HttpSession session, Model model) {
        Result result = new Result(200, "OK");
        boolean save = collegeService.updateById(college);
        if (!save) {
            result.setCode(-1);
            result.setMsg("修改失败");
        }
        return result;
    }

    @RequestMapping("/deleteCollege")
    @ResponseBody
    public Result deleteCollege(College college, HttpSession session, Model model) {
        Result result = new Result(200, "OK");
        boolean save = collegeService.removeById(college.getId());
        if (!save) {
            result.setCode(-1);
            result.setMsg("删除失败");
        }
        return result;
    }

    @RequestMapping("/getCollegeList")
    @ResponseBody
    public Result getCollegeList(HttpSession session, Model model) {
        Result result = new Result(200, "OK");
        List<College> list = collegeService.list(null);
        result.setData(list);
        return result;
    }

}
